﻿using System;
using System.Collections.Generic;
using System.Text;
using StockTrader.DAL.IDAL.HnxInfo.Oracle;
using StockTrader.BusinessService.DataContract.HnxInfo;
using System.Data.OleDb;
using System.Data;
using StockTrader.DAL.OracleHelper;
using StockTrader.DAL.IDAL.HnxInfo;

namespace StockTrader.DAL.Oracle.HnxInfo
{
    public class OHnxMarket : IOHnxMarket
    {
        private OleDbConnection _internalConnection;
        private OleDbTransaction _internalADOTransaction = null;

        public void Open(string connString)
        {
            if (_internalConnection == null)
                _internalConnection = new OleDbConnection(connString);
            if (_internalConnection.State != ConnectionState.Open)
                _internalConnection.Open();
        }
        public void Close()
        {
            if (_internalConnection != null && _internalConnection.State != System.Data.ConnectionState.Closed)
                _internalConnection.Close();
        }

        #region Parameters
        #endregion

        #region SQL string
        private const string GET_HNX_ALLMARKETINFO = "SELECT * FROM STS_MARKET_INFO WHERE FLOOR_CODE='02'";

        private const string GET_HNX_MARKETSTATUS = "SELECT * FROM STS_MARKET_INFO WHERE FLOOR_CODE='02'";
        #endregion

        #region IOHnxMarket Members


        public HnxMarketData getOHnxAllMarketInfo()
        {
            OleDbDataReader rdr = null;
            try
            {
                rdr = OleDbHelper.ExecuteReaderNoParm(_internalConnection, _internalADOTransaction, CommandType.Text, GET_HNX_ALLMARKETINFO);

                if (rdr.HasRows)
                {
                    rdr.Read();
                    return new HnxMarketData(
                        Convert.ToDateTime(rdr["TRADING_DATE"]),
                        rdr["TIME"].ToString(),
                        Convert.ToDecimal(rdr["CURRENT_STATUS"]),
                        Convert.ToDecimal(rdr["TOTAL_TRADE"]),
                        Convert.ToDecimal(rdr["TOTAL_STOCK"]),
                        Convert.ToDecimal(rdr["TOTAL_QTTY"]),
                        Convert.ToDecimal(rdr["TOTAL_VALUE"]),
                        Convert.ToDecimal(rdr["ADVANCES"]),
                        Convert.ToDecimal(rdr["NOCHANGE"]),
                        Convert.ToDecimal(rdr["DECLINES"]),
                        Convert.ToDecimal(rdr["CHG_INDEX"]),
                        Convert.ToDecimal(rdr["PCT_INDEX"]),
                        Convert.ToDecimal(rdr["MARKET_INDEX"]),
                        Convert.ToDecimal(rdr["BASE_INDEX"]),
                        Convert.ToDecimal(rdr["HIGHEST_INDEX"]),
                        Convert.ToDecimal(rdr["LOWEST_INDEX"]),
                        Convert.ToDecimal(rdr["DATE_NO"]),
                        Convert.ToDecimal(rdr["PT_TOTAL_QTTY"]),
                        rdr["PT_TOTAL_TRADE"].Equals(DBNull.Value) ? 0 :
                            Convert.ToDecimal(rdr["PT_TOTAL_TRADE"]),
                        rdr["PT_TOTAL_VALUE"].Equals(DBNull.Value) ? 0 :
                            Convert.ToDecimal(rdr["PT_TOTAL_VALUE"]));

                }
                else
                    return null;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            finally
            {
                rdr.Close();
            }
        }

        public int countOHnxMarketInfo()
        {
            throw new NotImplementedException();
        }

        public string getMarketStatus()
        {
            OleDbDataReader rdr = null;
            try
            {
                rdr = OleDbHelper.ExecuteReaderNoParm(_internalConnection, _internalADOTransaction, CommandType.Text, GET_HNX_MARKETSTATUS);

                if (rdr.HasRows)
                {
                    rdr.Read();
                    return rdr["CURRENT_STATUS"].ToString();
                }
                else
                    return string.Empty;
            }
            catch (System.Exception e)
            {
                throw e;
            }
            finally
            {
                rdr.Close();
            }
        }

        #endregion
    }
}
